import configparser
import os
import tensorflow as tf
from utils.lip_utils import LipPredictionMoel
from utils.leap_utils import LeapMotionPredictionMoel
from utils.speech_utils import SpeechPredictionMoel
from utils.all_utils import AllPredictionMoel
from GUI.gui import Application
# import warnings
# warnings.filterwarnings("ignore")

# 加载配置文件
conf=configparser.ConfigParser()
conf.read(os.path.join(os.getcwd(),"conf.ini"))
lip_weight_file=conf.get("Lip","weight_dir_path")
label_path=conf.get("Lip","label_path")
predict_frame_number=int(conf.get("Lip","predict_frame_number"))
threshold=float(conf.get("Lip","threshold"))
width=int(conf.get("Lip","width"))
height=int(conf.get("Lip","height"))
scaling_factor=float(conf.get("Lip","scaling_factor"))

svm_train_path=conf.get("LeapMotion","svm_train_path")

speech_weight_path=conf.get("Speech","speech_weight_path")
wav_path=conf.get("Speech","wav_path")
predict_threshold=float(conf.get("Speech","predict_threshold"))
loudness_threshold=int(conf.get("Speech","loudness_threshold"))
save_time=int(conf.get("Speech","save_time"))
chunk=int(conf.get("Speech","chunk"))
channels=int(conf.get("Speech","channels"))
rate=int(conf.get("Speech","rate"))

# 由于tensorflow默认图机制，所以当同时需要调用多个模型，需要创建多个图
graph_lip=tf.Graph()
graph_speech=tf.Graph()

print("load lip recognition model.....")
lip_model = LipPredictionMoel(weight_path=os.path.join(lip_weight_file,"weight_ckpt"),
                              label_path=label_path,
                              predict_frame_number=predict_frame_number,
                              threshold=threshold,scaling_factor=scaling_factor,
                              graph=graph_lip)
print("load lip recognition model finish")

print("load Gesture recognition model.....")
leap_model = LeapMotionPredictionMoel(svm_train_path=svm_train_path)
print("load Gesture recognition model finish")

print("load Speech recognition model.....")
speech_model = SpeechPredictionMoel(wav_path=wav_path,
                                    weight_path=speech_weight_path,
                                    predict_threshold=predict_threshold,
                                    loudness_threshold=loudness_threshold,
                                    save_time=save_time,
                                    chunk=chunk,
                                    channels=channels,
                                    rate=rate,
                                    graph=graph_speech)
print("load Speech recognition model finish")

all_model=AllPredictionMoel(lip_model=lip_model,
                            leap_model=leap_model,
                            speech_model=speech_model)

print("start run program GUI")
app = Application(width=width, height=height,
                  lip_prediction_model=lip_model,
                  leap_prediction_model=leap_model,
                  speech_prediction_model=speech_model,
                  all_prediction_model=all_model)
app.mainloop()
